using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;


namespace Microsoft.SharedSource.SqlServer.MDDE.Repository
{
    public sealed class MDDERepository
    {
        #region Fields

        SqlConnection repositoryConnection;
        PackageTemplateCollection packageTemplates = null;
        //PackageCollection repositoryPackages = null;

        #endregion

        #region Properties

        public SqlConnection RepositoryConnection
        {
            get
            {
                if (this.repositoryConnection.State != ConnectionState.Open)
                    this.repositoryConnection.Open();
                return this.repositoryConnection;
            }
        }

        public string RepositoryIdentifier
        {
            get
            {
                return this.RepositoryConnection.DataSource +
                    "." +
                    this.RepositoryConnection.Database;
            }
        }

        /// <summary>
        /// Package templates.
        /// </summary>
        public PackageTemplateCollection PackageTemplates
        {
            get
            {
                if (packageTemplates == null)
                {
                    RefreshPackageTemplates();
                }
                return packageTemplates;
            }
        }

        //public PackageCollection RepositoryPackages
        //{
        //    get
        //    {
        //        if (repositoryPackages == null)
        //        {
        //            RefreshRepositoryPackages();
        //        }
        //        return repositoryPackages;
        //    }
        //}

        #endregion

        #region Methods

        public MDDERepository(string repositoryConnectionString)
        {
            // Validate the input string.
            if (string.IsNullOrEmpty(repositoryConnectionString))
                throw new ArgumentException("Not a valid connection string.");

            // Create a new connection to MDDE repository.
            repositoryConnection = new SqlConnection(repositoryConnectionString);

            // Validate the existence of all the tables.
            if (!ValidateRepository())
                throw new ArgumentException("Not a valid repository");
        }

        private bool ValidateRepository()
        {
            // Check for Packages and PackageTemplates tables.
            // TODO: Also check for the rest of the tables.
            return Convert.ToBoolean(SqlHelper.ExecuteScalar(
                this.RepositoryConnection,
                CommandType.Text,
                Resource.CheckRepositoryTables));
        }

        internal void RefreshPackageTemplates()
        {
            // Get the template collection.
            DataSet templateName =
                SqlHelper.ExecuteDataset(this.RepositoryConnection,
                 CommandType.Text, Resource.GetPackageTemplateNames);

            if (packageTemplates == null)
                packageTemplates = new PackageTemplateCollection(this);

            for (int i = 0; i < templateName.Tables[0].Rows.Count; i++)
                if (!packageTemplates.ContainsTemplate(
                    templateName.Tables[0].Rows[i]["TemplateName"].ToString()))
                    packageTemplates.Add(new MDDEPackageTemplate(this,
                        templateName.Tables[0].Rows[i]["TemplateName"].ToString()));
        }

        #endregion
    }
}
